<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-10-14
 * Time: 下午2:18
 * To change this template use File | Settings | File Templates.
 */
class ControllerCatalogPackage extends Controller
{
    public function index()
    {
        $this->document->setTitle("套餐管理");
        $this->load->model('catalog/package');

        $this->getList();
    }

    public function insert() {
        $this->document->setTitle("套餐管理");

        $this->data['tab_general'] = "添加套餐";

        $this->load->model('catalog/package');

//        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
            $this->model_catalog_package->addpackage($this->request->post);

            $this->session->data['success'] = "成功： 您已成功添加套餐！";

            $this->redirect($this->url->link('catalog/package', 'token=' . $this->session->data['token'], 'SSL'));
        }
        $this->getForm();
    }
    public function update() {

        $this->document->setTitle("套餐管理");
        $this->data['tab_general'] = "编辑套餐";

        $this->load->model('catalog/package');

//        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
            $this->model_catalog_package->editpackage($this->request->get['id'], $this->request->post);

            $this->session->data['success'] = "成功： 您已成功更新套餐！";

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->redirect($this->url->link('catalog/package', 'token=' . $this->session->data['token'] . $url, 'SSL'));
        }

        $this->getForm();
    }

    public function delete() {
        $this->document->setTitle("套餐管理");


        $this->load->model('catalog/package');

        if (isset($this->request->post['selected'])) {
            foreach ($this->request->post['selected'] as $manufacturer_id) {
                $this->model_catalog_package->deletepackage($manufacturer_id);
            }
            $this->session->data['success'] = "成功： 您已成功删除套餐！";

//            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->redirect($this->url->link('catalog/package', 'token=' . $this->session->data['token'] . $url, 'SSL'));
        }

        $this->getList();
    }

    private function getList()
    {
        if (isset($this->request->get['sort'])) {
            $sort = $this->request->get['sort'];
        } else {
            $sort = 't.id';
        }

        if (isset($this->request->get['order'])) {
            $order = $this->request->get['order'];
        } else {
            $order = 'ASC';
        }

        if (isset($this->request->get['page'])) {
            $page = $this->request->get['page'];
        } else {
            $page = 1;
        }

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text' => "套餐管理",
            'href' => $this->url->link('catalog/package', 'token=' . $this->session->data['token'] . $url, 'SSL'),
            'separator' => ' :: '
        );

        $this->data['insert'] = $this->url->link('catalog/package/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
        $this->data['delete'] = $this->url->link('catalog/package/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');

        $this->data['packages'] = array();

        $data = array(
            'sort' => $sort,
            'order' => $order,
            'start' => ($page - 1) * $this->config->get('config_admin_limit'),
            'limit' => $this->config->get('config_admin_limit')
        );

        $attribute_total = $this->model_catalog_package->getTotalpackages();

        $results = $this->model_catalog_package->getPackages($data);

        foreach ($results as $result) {
            $action = array();

            $action[] = array(
                'text' => $this->language->get('text_edit'),
                'href' => $this->url->link('catalog/package/update', 'token=' . $this->session->data['token'] . '&id=' . $result['id'] . $url, 'SSL')
            );

            $this->data['packages'][] = array(
                'id' => $result['id'],
                'name' => $result['name'],
                'description' => $result['description'],
                'selected' => isset($this->request->post['selected']) && in_array($result['id'], $this->request->post['selected']),
                'action' => $action
            );
        }

        $this->data['heading_title'] = "套餐管理";

        $this->data['text_no_results'] = $this->language->get('text_no_results');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->session->data['success'])) {
            $this->data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $this->data['success'] = '';
        }

        $url = '';

        if ($order == 'ASC') {
            $url .= '&order=DESC';
        } else {
            $url .= '&order=ASC';
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        $pagination = new Pagination();
        $pagination->total = $attribute_total;
        $pagination->page = $page;
        $pagination->limit = $this->config->get('config_admin_limit');
        $pagination->text = $this->language->get('text_pagination');
        $pagination->url = $this->url->link('catalog/package', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

        $this->data['pagination'] = $pagination->render();

        $this->data['sort'] = $sort;
        $this->data['order'] = $order;

        $this->template = 'catalog/package_list.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    private function getForm() {

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->error['name'])) {
            $this->data['error_name'] = $this->error['name'];
        } else {
            $this->data['error_name'] = '';
        }

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => "套餐管理",
            'href'      => $this->url->link('catalog/package', 'token=' . $this->session->data['token'] . $url, 'SSL'),
            'separator' => ' :: '
        );

        if (!isset($this->request->get['id'])) {
            $this->data['action'] = $this->url->link('catalog/package/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
        } else {
            $this->data['action'] = $this->url->link('catalog/package/update', 'token=' . $this->session->data['token'] . '&id=' . $this->request->get['id'] . $url, 'SSL');
        }

        $this->data['cancel'] = $this->url->link('catalog/package', 'token=' . $this->session->data['token'] . $url, 'SSL');

        if (isset($this->request->get['id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
            $manufacturer_info = $this->model_catalog_package->getpackage($this->request->get['id']);
        }

        $this->data['token'] = $this->session->data['token'];

        if (isset($this->request->post['name'])) {
            $this->data['name'] = $this->request->post['name'];
        } elseif (!empty($manufacturer_info)) {
            $this->data['name'] = $manufacturer_info['name'];
        } else {
            $this->data['name'] = '';
        }

        if (isset($this->request->post['description'])) {
            $this->data['description'] = $this->request->post['description'];
        } elseif (!empty($manufacturer_info)) {
            $this->data['description'] = $manufacturer_info['description'];
        } else {
            $this->data['description'] = '';
        }
        if (isset($this->request->post['option_value'])) {
            $option_values = $this->request->post['option_value'];
        } elseif (isset($this->request->get['id'])) {
            $option_values = $this->model_catalog_package->getOptionValueDescriptions($this->request->get['id']);
        } else {
            $option_values = array();
        }

        $this->load->model('tool/image');

        $this->data['option_values'] = array();

        foreach ($option_values as $option_value) {
//            if ($option_value['image'] && file_exists(DIR_IMAGE . $option_value['image'])) {
//                $image = $option_value['image'];
//            } else {
//                $image = 'no_image.jpg';
//            }

            $this->data['option_values'][] = array(
                'id'          => $option_value['id'],
                'name'          => $option_value['name'],
                'month_price'          => $option_value['month_price'],
                'return_money'          => $option_value['return_money'],
                'give_money'          => $option_value['give_money'],
                'time' => $option_value['time']
            );
        }
//
//        $this->load->model('tool/image');
//
//        if (isset($this->request->post['image']) && file_exists(DIR_IMAGE . $this->request->post['image'])) {
//            $this->data['thumb'] = $this->model_tool_image->resize($this->request->post['image'], 100, 100);
//        } elseif (!empty($manufacturer_info) && $manufacturer_info['image'] && file_exists(DIR_IMAGE . $manufacturer_info['image'])) {
//            $this->data['thumb'] = $this->model_tool_image->resize($manufacturer_info['image'], 100, 100);
//        } else {
//            $this->data['thumb'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
//        }
//
//        $this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
//
//        if (isset($this->request->post['sort_order'])) {
//            $this->data['sort_order'] = $this->request->post['sort_order'];
//        } elseif (!empty($manufacturer_info)) {
//            $this->data['sort_order'] = $manufacturer_info['sort_order'];
//        } else {
//            $this->data['sort_order'] = '';
//        }

        $this->template = 'catalog/package_form.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    private function validateForm() {
        if (!$this->user->hasPermission('modify', 'catalog/package')) {
            $this->error['warning'] = "警告： 您没有权限更改套餐！";
        }

        if (utf8_strlen($this->request->post['package']) != 11) {
            $this->error['name'] = "套餐必须是11位";
        }

//        if ($this->error && !isset($this->error['warning'])) {
//            $this->error['warning'] = "警告： 相关数据未键入！";
//        }
        if (!$this->error) {
            return true;
        } else {
            return false;
        }

    }
}